<?php
namespace app\controllers\api\account;

use fate\http\Request;
use app\libs\LibUser;
use app\models\UserCreditLog;

class ListcreditlogController extends \fate\web\PureController {
    public function run() {
        global $_G;

        $req = Request::getInstance();

        $page = $req->getQueryString('page');

        if(!is_numeric($page)) {
            $page = 1;
        }

        $uid = LibUser::getLoginUid();
        if(!is_numeric($uid)) {
            $this->ajaxReturn(null, 300002);
            exit;
        }

        $model = new UserCreditLog();
        $day30 = $_G['now_time'] - 60 * 60 * 24 * 30;
        $where = 'uid=' . $uid . ' and log_time > ' . $day30;
        $pageSize = 10;

        $total = $model->count($where);
        $list = $model->getListByCondition('id,money,score,experience,action,log_time,reason', $where, $pageSize, 'id desc');

        if(null !== $list) {
            for($i=0; $i<count($list); $i++) {
                $list[$i]['action_name'] = UserCreditLog::getActionName($list[$i]['action']);
            }
        }

        $this->ajaxReturn([
            'data' => $list,
            'page_size' => $pageSize,
            'current' => $page,
            'total' => $total
        ]);
    }
}
